/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProfesorUI.java
 *
 * Created on Mar 17, 2010, 3:28:43 PM
 */

package edu.fca.ui;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import edu.fca.beans.CalendarioBean;
import edu.fca.beans.ProfesorBean;
import edu.fca.ui.exceptions.InvalidCodeException;
import edu.fca.ui.exceptions.InvalidLastNameException;
import edu.fca.ui.exceptions.InvalidNameException;
import edu.fca.ui.search.SearchProfesorUI;
import edu.fca.ui.tools.AlertManager;
import edu.fca.ui.tools.ComponentManager;
import edu.fca.ui.tools.Validations;
import java.sql.Date;
import java.sql.Time;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import logic.services.CalendarioServices;
import logic.services.exceptions.InvalidDateException;
import logic.services.exceptions.MaxNumberOfCalendarsException;

/**
 *
 * @author Romms
 */
public class CalendarioUI extends javax.swing.JInternalFrame {

    /** Creates new form ProfesorUI */
    private CalendarioBean calendarioBean;

    private enum Action{
        ADD,UPDATE,DELETE
    }

    private String oldCode;
    private Action action;

    public CalendarioUI() {
        initComponents();
        this.fillTime();
        startMenuBar();
        calendarioBean = new CalendarioBean();
        ComponentManager.enableDisableComponentesIn(this.getContentPane().getComponents(), false);
        ComponentManager.enableDisableComponentesIn(this.pnlCalendario.getComponents(), false);
        ComponentManager.enableDisableComponentesIn(this.pnlProfesor.getComponents(), false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tlbMenuBarra = new javax.swing.JToolBar();
        btnNuevo = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnBorrar = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        btnAcept = new javax.swing.JButton();
        pnlProfesor = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtCodigoProfesor = new javax.swing.JTextField();
        btnBuscarProfesor = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtProfesor = new javax.swing.JTextField();
        pnlCalendario = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        cmbMM = new javax.swing.JComboBox();
        cmbFecha = new datechooser.beans.DateChooserCombo();
        txtCodigoCalendario = new javax.swing.JTextField();
        cmbHH = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setClosable(true);
        setTitle("Calendario");

        tlbMenuBarra.setRollover(true);

        btnNuevo.setText("Nuevo");
        btnNuevo.setFocusable(false);
        btnNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });
        tlbMenuBarra.add(btnNuevo);

        btnEditar.setText("Editar");
        btnEditar.setFocusable(false);
        btnEditar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEditar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });
        tlbMenuBarra.add(btnEditar);

        btnBorrar.setText("Borrar");
        btnBorrar.setFocusable(false);
        btnBorrar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBorrar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBorrarActionPerformed(evt);
            }
        });
        tlbMenuBarra.add(btnBorrar);

        btnBuscar.setText("Buscar");
        btnBuscar.setFocusable(false);
        btnBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        tlbMenuBarra.add(btnBuscar);

        btnCancelar.setText("Cancelar");
        btnCancelar.setFocusable(false);
        btnCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });
        tlbMenuBarra.add(btnCancelar);

        btnAceptar.setText("Aceptar");
        btnAceptar.setFocusable(false);
        btnAceptar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAceptar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });
        tlbMenuBarra.add(btnAceptar);

        btnCancel.setText("Cancelar");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnAcept.setText("Aceptar");
        btnAcept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptActionPerformed(evt);
            }
        });

        pnlProfesor.setBorder(javax.swing.BorderFactory.createTitledBorder("Profesor:"));

        jLabel5.setText("Nombre:");

        txtCodigoProfesor.setEditable(false);

        btnBuscarProfesor.setText("Buscar Profesor");
        btnBuscarProfesor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarProfesorActionPerformed(evt);
            }
        });

        jLabel4.setText("Código:");

        txtProfesor.setEditable(false);

        javax.swing.GroupLayout pnlProfesorLayout = new javax.swing.GroupLayout(pnlProfesor);
        pnlProfesor.setLayout(pnlProfesorLayout);
        pnlProfesorLayout.setHorizontalGroup(
            pnlProfesorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlProfesorLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlProfesorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(pnlProfesorLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(14, 14, 14)
                        .addComponent(txtCodigoProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnBuscarProfesor))
                    .addGroup(pnlProfesorLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtProfesor)))
                .addContainerGap(16, Short.MAX_VALUE))
        );
        pnlProfesorLayout.setVerticalGroup(
            pnlProfesorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlProfesorLayout.createSequentialGroup()
                .addGroup(pnlProfesorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlProfesorLayout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel4))
                    .addGroup(pnlProfesorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtCodigoProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnBuscarProfesor)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlProfesorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnlCalendario.setBorder(javax.swing.BorderFactory.createTitledBorder("Calendario:"));

        jLabel6.setText(":");

        cmbFecha.setNothingAllowed(false);
        cmbFecha.setFormat(0);
        cmbFecha.setLocale(new java.util.Locale("es", "MX", ""));

        txtCodigoCalendario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtCodigoCalendarioFocusLost(evt);
            }
        });
        txtCodigoCalendario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCodigoCalendarioKeyReleased(evt);
            }
        });

        jLabel2.setText("Fecha:");

        jLabel1.setText("Código:");

        jLabel3.setText("Hora:");

        javax.swing.GroupLayout pnlCalendarioLayout = new javax.swing.GroupLayout(pnlCalendario);
        pnlCalendario.setLayout(pnlCalendarioLayout);
        pnlCalendarioLayout.setHorizontalGroup(
            pnlCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCalendarioLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtCodigoCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(139, 139, 139))
            .addGroup(pnlCalendarioLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addContainerGap(227, Short.MAX_VALUE))
            .addGroup(pnlCalendarioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(pnlCalendarioLayout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addGroup(pnlCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cmbFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(pnlCalendarioLayout.createSequentialGroup()
                                .addComponent(cmbHH, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmbMM, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        pnlCalendarioLayout.setVerticalGroup(
            pnlCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCalendarioLayout.createSequentialGroup()
                .addGroup(pnlCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCodigoCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(cmbFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmbHH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(cmbMM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(11, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tlbMenuBarra, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(pnlProfesor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnlCalendario, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(140, Short.MAX_VALUE)
                .addComponent(btnCancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAcept)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tlbMenuBarra, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlProfesor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnAcept))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        action=Action.ADD;
        
        if(this.btnAceptar.isEnabled()){
            int answer=AlertManager.showOkCancelAlert("Registro sin guardar.", "Actualmente está editando un registro, " +
                    "¿Desea borrar la información?");
            if(answer!=0) return;
        }

        ComponentManager.cleanAllInTextFieldsIn(this.getContentPane().getComponents());
        ComponentManager.enableDisableComponentesIn(this.getContentPane().getComponents(), true);
        ComponentManager.enableDisableComponentesIn(this.pnlCalendario.getComponents(), true);
        ComponentManager.enableDisableComponentesIn(this.pnlProfesor.getComponents(), true);
        this.btnAceptar.setEnabled(true);
        this.btnCancelar.setEnabled(true);

        this.btnEditar.setEnabled(false);
        this.btnBorrar.setEnabled(false);
        this.btnBuscar.setEnabled(false);

        this.txtCodigoCalendario.requestFocus();
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void fillCalendarioBean(){
        this.calendarioBean.setCodigo(this.txtCodigoCalendario.getText());
        this.calendarioBean.setProfesorCodigo(this.txtCodigoProfesor.getText());
        this.calendarioBean.setFecha(this.getDateFrom());
        this.calendarioBean.setHora(this.getTime());
    }

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        try {
            validateData();
            fillCalendarioBean();

            doAction(action);

            ComponentManager.cleanAllInTextFieldsIn(this.getContentPane().getComponents());
            ComponentManager.cleanAllInTextFieldsIn(this.pnlCalendario.getComponents());
            ComponentManager.cleanAllInTextFieldsIn(this.pnlProfesor.getComponents());

            ComponentManager.enableDisableComponentesIn(this.getContentPane().getComponents(), false);
            ComponentManager.enableDisableComponentesIn(this.pnlCalendario.getComponents(), false);
            ComponentManager.enableDisableComponentesIn(this.pnlProfesor.getComponents(), false);
            startMenuBar();

        } catch (InvalidDateException ex) {
            AlertManager.showAlert("Fecha inválida.",
                    ex.getMessage());
        }catch (MaxNumberOfCalendarsException ex) {
            AlertManager.showAlert("Imposible asignar al profesor.",
                    ex.getMessage());
            this.txtCodigoCalendario.select(0, this.txtCodigoProfesor.getText().length());
        }catch (MySQLIntegrityConstraintViolationException ex) {
            AlertManager.showAlert("Código inválido.", "El código "+this.txtCodigoCalendario.getText()+" ya existe.");
            this.txtCodigoCalendario.select(0, this.txtCodigoCalendario.getText().length());
        }catch (InvalidLastNameException ex) {
            AlertManager.showAlert("Apellido paterno inválido.", ex.getExceptionMessage());
        } catch (InvalidNameException ex) {
            AlertManager.showAlert("Nombre inválido.", ex.getExceptionMessage());
        } catch (InvalidCodeException ex) {
            AlertManager.showAlert("Código inválido", ex.getExceptionMessage());
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        ComponentManager.cleanAllInTextFieldsIn(this.getContentPane().getComponents());
        ComponentManager.enableDisableComponentesIn(this.getContentPane().getComponents(), false);
        startMenuBar();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void txtCodigoCalendarioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtCodigoCalendarioFocusLost

    }//GEN-LAST:event_txtCodigoCalendarioFocusLost

    private void btnAceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptActionPerformed
        this.btnAceptarActionPerformed(evt);
    }//GEN-LAST:event_btnAceptActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.btnCancelarActionPerformed(evt);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void txtCodigoCalendarioKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCodigoCalendarioKeyReleased
        
    }//GEN-LAST:event_txtCodigoCalendarioKeyReleased

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        SearchProfesorUI searchProfesorUI=new SearchProfesorUI(null, true);
        searchProfesorUI.setVisible(true);
        searchProfesorUI.dispose();
        //this.calendarioBean=searchProfesorUI.getSelectedProfesor();
        //oldCode=searchCalendarioUI.getSelectedCalendario();
        //fillCalendarioFields(calendarioBean);
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnBuscarProfesorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarProfesorActionPerformed
        SearchProfesorUI searchProfesorUI=new SearchProfesorUI(null, true);
        searchProfesorUI.setVisible(true);
        searchProfesorUI.dispose();
        ProfesorBean profesor=searchProfesorUI.getSelectedProfesor();
        this.txtCodigoProfesor.setText(profesor.getCodigo());
        this.txtProfesor.setText(profesor.getNombre() + " " + profesor.getApellidoP());
    }//GEN-LAST:event_btnBuscarProfesorActionPerformed

    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        action=Action.UPDATE;
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBorrarActionPerformed
        action=Action.DELETE;
    }//GEN-LAST:event_btnBorrarActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAcept;
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnBorrar;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnBuscarProfesor;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnNuevo;
    private datechooser.beans.DateChooserCombo cmbFecha;
    private javax.swing.JComboBox cmbHH;
    private javax.swing.JComboBox cmbMM;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel pnlCalendario;
    private javax.swing.JPanel pnlProfesor;
    private javax.swing.JToolBar tlbMenuBarra;
    private javax.swing.JTextField txtCodigoCalendario;
    private javax.swing.JTextField txtCodigoProfesor;
    private javax.swing.JTextField txtProfesor;
    // End of variables declaration//GEN-END:variables

    private void startMenuBar(){
        btnNuevo.setEnabled(true);
        btnBuscar.setEnabled(true);

        btnEditar.setEnabled(false);
        btnBorrar.setEnabled(false);
        btnCancelar.setEnabled(false);
        btnAceptar.setEnabled(false);
    }

    private void validateData() throws InvalidCodeException, InvalidNameException, InvalidLastNameException{
        if(!Validations.textFieldSizeEqual(this.txtCodigoCalendario, 5)){
            this.txtCodigoCalendario.requestFocus();
            this.txtCodigoCalendario.select(0, this.txtCodigoCalendario.getText().length());
            throw new InvalidCodeException("Código de calendario inválido, máximo 5 caracteres.");
        }


        if(this.txtCodigoProfesor.getText().isEmpty()){
            this.txtCodigoProfesor.requestFocus();
            throw new InvalidNameException("El código del profesor no puede ser vacío.");
        }

    }

    private Date getDateFrom(){
            int anio = this.cmbFecha.getSelectedDate().get(Calendar.YEAR);
            int dia = this.cmbFecha.getSelectedDate().get(Calendar.DAY_OF_MONTH);
            int mes = this.cmbFecha.getSelectedDate().get(Calendar.MONTH);
            int hora= Integer.parseInt(this.cmbHH.getSelectedItem().toString());
            int minuto=Integer.parseInt(this.cmbMM.getSelectedItem().toString());
            Calendar cal = new GregorianCalendar(anio, mes, dia,hora,minuto) {};

            return new java.sql.Date( cal.getTime().getTime() );
    }

    private void fillTime(){
        this.cmbHH.addItem("HH");
        for(int i=0;i<24;i++){
            this.cmbHH.addItem(i);
        }

        this.cmbMM.addItem("MM");
        for(int i=0;i<60;i++){
            if(String.valueOf(i).length()<2){
                this.cmbMM.addItem("0"+String.valueOf(i));
            }else{
                this.cmbMM.addItem(i);
            }
        }
    }

    private Time getTime(){
            int anio = this.cmbFecha.getSelectedDate().get(Calendar.YEAR);
            int dia = this.cmbFecha.getSelectedDate().get(Calendar.DAY_OF_MONTH);
            int mes = this.cmbFecha.getSelectedDate().get(Calendar.MONTH);
            int hora= Integer.parseInt(this.cmbHH.getSelectedItem().toString());
            int minuto=Integer.parseInt(this.cmbMM.getSelectedItem().toString());
            Calendar cal = new GregorianCalendar(anio, mes, dia,hora,minuto) {};

            return new Time(cal.getTimeInMillis());
    }


    private void doAction(Action action) throws MySQLIntegrityConstraintViolationException, MaxNumberOfCalendarsException, InvalidDateException{
            switch(action){
            case ADD:
                CalendarioServices.getInstance().addCalendario(calendarioBean);
            break;
            case UPDATE:
                CalendarioServices.getInstance().updateCalendario(calendarioBean, this.oldCode);
            break;
            case DELETE:
                
            break;

        }
    }
}

